package com.tanhua.server.controller;

import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.server.service.VideoMQService;
import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName :  VideoController
 * @Author : Tenebrous
 * @Date: 2020/12/15 18:49
 * @Description : 小视频表现层
 */
@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private CommentsController commentsController;

    @Autowired
    private VideoService videoService;

    @Autowired
    private MovementController movementController;

    @Autowired
    private VideoMQService videoMQService;

    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
                                          @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        try {
            System.out.println(picFile);
            String id = this.videoService.saveVideo(picFile, videoFile);
            if(StringUtils.isNotEmpty(id)){
                // 发送消息
                this.videoMQService.videoMsg(id);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询小视频列表
     *
     * @param page              页码
     * @param pageSize          每页条数
     * @return                  状态码
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            if (page <= 0) {
                page = 1;
            }
            PageResult pageResult = this.videoService.queryVideoList(page, pageSize);
            if (null != pageResult) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频点赞
     *
     * @param videoId 视频id
     * @return          状态码和点赞数量
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementController.likeComment(videoId);
        if (entity.getStatusCode().is2xxSuccessful()) {
            // 发送消息
            this.videoMQService.likeVideoMsg(videoId);
        }

        return entity;
    }

    /**
     * 取消点赞
     *
     * @param videoId       视频id
     * @return              状态码和点赞数量
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementController.disLikeComment(videoId);
        if (entity.getStatusCode().is2xxSuccessful()) {
            this.videoMQService.disLikeVideoMsg(videoId);
        }

        return this.movementController.disLikeComment(videoId);
    }

    /**
     *查询评论列表
     * @param videoId           视频id
     * @param page              页码
     * @param pagesize          每页大小
     * @return                  状态码
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        ResponseEntity<PageResult> entity = this.commentsController.queryCommentsList(videoId, page, pagesize);
        return this.commentsController.queryCommentsList(videoId, page, pagesize);
    }

    /**
     * 提交评论
     *
     * @param param                 评论正文
     * @param videoId               视频id
     * @return                      状态码
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param,
                                             @PathVariable("id") String videoId) {
        param.put("movementId", videoId);
        ResponseEntity<Void> entity = this.commentsController.saveComments(param);
        if (entity.getStatusCode().is2xxSuccessful()) {
            this.videoMQService.commentVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 评论点赞
     *
     * @param publishId     动态id
     * @return              状态码
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String publishId) {
        return this.movementController.likeComment(publishId);
    }

    /**
     * 评论取消点赞
     *
     * @param publishId             动态id
     * @return                      状态码
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String publishId) {
        return this.movementController.disLikeComment(publishId);
    }


}
